package com.mikao.dto.vo;

import com.mikao.constant.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * API返回值基础包装类
 *
 * @author KennyDavid
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BaseResult<T> {
    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误提示信息
     */
    private String msg;

    /**
     * 表单校验错误提示信息
     */
    private List<String> errors;

    /**
     * 返回值
     */
    private T data;

    public BaseResult(T data) {
        this.data = data;
        this.code = ErrorCode.SUCCESS.getCode();
        this.msg = ErrorCode.SUCCESS.getMsg();
    }

    public BaseResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static BaseResult getDefaultSuccessResult() {
        return BaseResult.builder()
                .code(ErrorCode.SUCCESS.getCode())
                .msg(ErrorCode.SUCCESS.getMsg())
                .build();
    }

    public static BaseResult getDefaultFailureResult() {
        return BaseResult.builder()
                .code(ErrorCode.FAILURE.getCode())
                .msg(ErrorCode.FAILURE.getMsg())
                .build();
    }

    public static BaseResult getParamErrorResult() {
        return BaseResult.builder()
                .code(ErrorCode.PARAM_ERROR.getCode())
                .msg(ErrorCode.PARAM_ERROR.getMsg())
                .build();
    }
}
